<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="sopt.engSearch.TranslateProc"%>
<%@page import="comm.util.SoptHashMap"%>
<%@page import="comm.util.RequestManager"%> 
<%@page import="statics.SoptVars"%>
<%@page import="sopt.engSearch.NaverSearchProc"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="sopt.engSearch.QueryDataProc"%>
<%@page import="sopt.engSearch.ESKoreaProc"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.regex.Matcher"%>   
<%
	request.setCharacterEncoding("UTF-8");
	SoptHashMap hm = RequestManager.getRequest(request);
	if(hm==null || hm.get("Q")==null){
		response.sendRedirect("./");
	}
	String currentCategory = "";
	if(hm.get("CATEGORY")!=null && hm.get("CATEGORY").toString().length()>0){
		currentCategory = hm.get("CATEGORY").toString();
	}else{
		currentCategory = "Knowledge";
	}
	SoptHashMap shortcutHm = null;
	ArrayList lt = null;
	ArrayList recentLt = null;
	ArrayList mostLt = null;
	ArrayList koreanLt = null;
	ArrayList abroadLt = null;
	
	System.out.println("RESULT 에 해쉬맵!+ㅁ+"+hm.toString());
	String translatedQ = "";
	String q = "";
	String encodedQ = "";
	SoptHashMap suggestMistakeWorld  = null;
	int length = 5;
	if(hm.get("Q")!=null){
		
		q = hm.get("Q").toString();
		
		String pattern = "[a-zA-Z'\\s]";
		StringBuffer sb = new StringBuffer();

		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(q);

		System.out.println("source:"+q);

		while(m.find()) {
			System.out.println(m.group()+"@@@@@");
			sb.append(m.group());
		}
		
		q = sb.toString(); 
		
		
		QueryDataProc queryData = new QueryDataProc();
		String curIp = "";
		if(!request.getRemoteAddr().toString().equals("127.0.0.1")){
			curIp = queryData.getUserCountry(request.getRemoteAddr());
			queryData.regQuery(q,curIp);
		}
		System.out.println(q);
		
		encodedQ = URLEncoder.encode(q);
		System.out.println(encodedQ);
		System.out.println("===========================================여기!!!");
		//관광공사DB부터 조사후 번역을 할지 말지 결정
		ESKoreaProc proc = new ESKoreaProc();
		
		translatedQ = proc.getKoreaSparklingWord(q);
		
		suggestMistakeWorld = proc.getMistakeWord(q); 
		
		if(translatedQ==null || translatedQ.length()<1){
			translatedQ = TranslateProc.getTranslatedString(q,SoptVars.ENG_TO_KOR);
		}
		
		shortcutHm = NaverSearchProc.getShortcutUrl(q);
		if(shortcutHm.get("LINK").toString().length()==0)
			shortcutHm = NaverSearchProc.getShortcutUrl(translatedQ);
		
		lt = NaverSearchProc.getSuggestQueryList(translatedQ,length);
		
	}
%>
	<div id="header">
		<span style="float:left;width:90px;">
			<a href="./"><img src="../images/logo.png" alt="ESKorea logo" title="ESKorea" width="80"/></a>
		</span>
		
		<div id="categoryMenu">
			<ul id="categoryTab">
				<li id="sbar_knowledge" <%if(currentCategory.equals("Knowledge")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Knowledge')" class="sbTab tabTxt">
						<span class="sp_knowledge">
							<%if(currentCategory.equals("Knowledge")){%>
								<strong>Knowledge</strong>
							<%}else{ %>
								Knowledge
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Cafe")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Cafe')" class="sbTab tabTxt">
						<span class="sp_blog">
							<%if(currentCategory.equals("Cafe")){%>
								<strong>Cafe</strong>
							<%}else{ %>
								Cafe
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Bulletins")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Bulletins')" class="sbTab tabTxt">
						<span class="sp_bulletins">
							 <!-- 게시판 -->
							<%if(currentCategory.equals("Bulletins")){%>
								<strong>Bulletins</strong>
							<%}else{ %>
								Bulletins
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Blog")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Blog')" class="sbTab tabTxt">
						<span class="sp_blog">
							<%if(currentCategory.equals("Blog")){%>
								<strong>Blog</strong>
							<%}else{ %>
								Blog
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Image")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Image')" class="sbTab tabTxt">
						<span class="sp_image">
							
							<%if(currentCategory.equals("Image")){%>
								<strong>Image</strong>
							<%}else{ %>
								Image
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Videos")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Videos')" class="sbTab tabTxt">
						<span class="sp_video">
							<%if(currentCategory.equals("Videos")){%>
								<strong>Videos</strong>
							<%}else{ %>
								Videos
							<%} %>
						</span>
					</a>
				</li>
				<li <%if(currentCategory.equals("Local")){%>class="menu_on"<%}%>>
					<a href="javascript:getSearchResultCategoryQuery('<%=encodedQ%>','Local')" class="sbTab tabTxt">
						<span>
							<%if(currentCategory.equals("Local")){%>
								<strong>Local</strong>
							<%}else{ %>
								Local
							<%} %>
						</span>
					</a>
				</li>
			</ul>
		</div>
		<div style="float:left;padding-top:15px;padding-left:5px;"><a style="font-size:11px;" href="http://www.daum.net">powerd by Daum</a></div>
		<div id="topMenuLayer">
			<a href="./aboutk.jsp">About(KOR)</a>
			|
			<a href="./aboute.jsp">About(ENG)</a>
			|
			<a href="./help.jsp">Help</a>
			|
			<a href="./credits.jsp">Credits</a>
		</div>
	</div>
	
	<div id="container">
		<div id="resultHeader">				
			<div id="resultSearchBar">
				<form method="post" onsubmit="javascript:return getSearchResult();">
				<input maxlength="2048" id="queryInput"
					type="text" name="q" size="25" 
					title="English Search For Visitor to Korea" value="<%=q%>">
				<input type="hidden" id="curCategory" name="category" value="<%=currentCategory%>" />
				<input type="hidden" id="queryInput" value="<%=translatedQ%>" />
				<input type="submit" id="searchBtn" value="Search"/>
				</form>
			</div>
			<div id="resultSearchedQuery">
				<span style="float:left;">Current Query : <img src="../images/ico_eng.png" width="17" height="17"/><%=q%></span>
				<span style="float:left;width:2px;padding:0 5px;"></span>
				<span style="float:left;"><img src="../images/ico_kor.png" width="17" height="17"/><%=translatedQ %></span>
			</div>
			<div class="clear"></div>
			
		</div>
		
		<div id="resultContent">
			<%if(suggestMistakeWorld!=null 
					&& suggestMistakeWorld.get("NAME")!=null
					&& !q.toLowerCase().equals(suggestMistakeWorld.get("TRANSLATED_NAME").toString().toLowerCase())){ %>
				<div>
					<span style="color:#CC0000;font-size:12px;">Did you find this?</span>
					<a href="./result.jsp?q=<%=URLEncoder.encode(suggestMistakeWorld.get("TRANSLATED_NAME").toString(),"UTF-8")%>"><%=suggestMistakeWorld.get("TRANSLATED_NAME")%></a>
				</div>
			<%} %>
			
			<%if(shortcutHm!=null && shortcutHm.get("LINK")!=null && shortcutHm.get("LINK").toString().length()>0){%>
			<div id="resultShortCut" class="resultContentCategory">
				<span>Shortcut :
					<img src="../images/ico_eng.png" width="17" height="17"/>
					<b><%=TranslateProc.getTranslatedString(shortcutHm.get("LABEL").toString(),SoptVars.KOR_TO_ENG)%></b>
					<img src="../images/ico_kor.png" width="17" height="17"/>
					<%=shortcutHm.get("LABEL")%>
					<a href='<%=shortcutHm.get("LINK")%>'><%=shortcutHm.get("LINK")%></a>
					 by ®Naver
				</span>
			</div>
			<%} %>
			
			<div id="relatedkeywordLayer" class="resultContentCategory">
				Realated keyword :
				<%if(lt!=null&&lt.size()>0){ %>
					<%for(int i=0;i<lt.size();i++){ 
						String tempTrans = TranslateProc.getTranslatedString(lt.get(i).toString(),SoptVars.KOR_TO_ENG); %>
						<a href="javascript:getSearchResultQuery('<%=URLEncoder.encode(tempTrans)%>')">
							<%=tempTrans%>
						</a>
						<span style="width:1px;padding:0 2px;"></span>
					<%} %>
				<%} %>
				<%
				QueryDataProc query = new QueryDataProc();
			
				recentLt = query.getRecentQueryList();
				//mostLt = query.getMostQueryList();
				koreanLt = query.getKoreanQueryList();
				abroadLt = query.getAbroadQueryList();
				%>
				<div id="ourQueryLayer">
					<div>
						<div class="ourQueryHeader">Recent Search Keywords</div>
						<div class="ourQueryContent">
							<ul>
								<%if(recentLt!=null)for(int i=0;i<recentLt.size();i++){ %>
									<li><a href="javascript:getSearchResultQuery('<%=URLEncoder.encode(recentLt.get(i).toString(),"UTF-8")%>')"><%=recentLt.get(i) %></a></li>
								<%} %>
							</ul>
						</div>
					</div>
					
					
					<div id="countryQueryLayer">
						<div class="ourQueryHeader">
							<div id="ourQueryMostHeader">Most Keywords Korean</div>
							<div class="clear"></div>
						</div>
						<div class="ourQueryContent">
							<ul>
							<%if(koreanLt!=null)for(int i=0;i<koreanLt.size();i++){ SoptHashMap nMap = (SoptHashMap)koreanLt.get(i);%>
								<li><a href="javascript:getSearchResultQuery('<%=URLEncoder.encode(nMap.get("QUERY_TEXT").toString(),"UTF-8")%>')"><%=nMap.get("QUERY_TEXT")%></a>
								</li>
							<%} %>
							</ul>
							<div style="font-size:8px;text-align:right;">provided by <a style="font-size:9px;" href="http://www.webhosting.info">WebHosting.Info</a></div>
						</div>
						<!--
							"This 'work' uses the IP-to-Country Database
							 provided by WebHosting.Info (http://www.webhosting.info),
							 available from http://ip-to-country.webhosting.info." 
						 -->
					</div>
					
					<div id="countryQueryLayer">
						<div class="ourQueryHeader">
							<div id="ourQueryMostHeader">Most Keywords Abroad</div>
							<div class="clear"></div>
						</div>
						<div class="ourQueryContent">
							<ul>
							<%if(abroadLt!=null)for(int i=0;i<abroadLt.size();i++){ SoptHashMap nMap = (SoptHashMap)abroadLt.get(i);%>
								<li><a href="javascript:getSearchResultQuery('<%=URLEncoder.encode(nMap.get("QUERY_TEXT").toString(),"UTF-8")%>')"><%=nMap.get("QUERY_TEXT")%></a>
									<span style="float:right;"><%=nMap.get("COUNTRY")%></span>
								</li>
							<%} %>
							</ul>
							<div style="font-size:8px;text-align:right;">provided by <a style="font-size:9px;" href="http://www.webhosting.info">WebHosting.Info</a></div>
						</div>
						<!--
							"This 'work' uses the IP-to-Country Database
							 provided by WebHosting.Info (http://www.webhosting.info),
							 available from http://ip-to-country.webhosting.info." 
						 -->
					</div>
				</div>
			</div>
			
			<div id="resultLayer" class="resultContentCategory"></div>
			
			
			
			<div id="resultLoadingBar">
				<div id="flashLoading">
					<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="440" height="370" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="visualzone" align="middle">
					<param name="allowScriptAccess" value="always" />
					<param name="allowFullScreen" value="false" />
					<param name="movie" value="../images/loading.swf" />
					<param name="quality" value="high" />
					<param name="bgcolor" value="#FFFFFF" />
					<param name="menu" value="false" />
					<param name="wmode" value="transparent" />
					<embed src="../images/loading.swf" wmode="transparent" width="440" height="370" quality="high" bgcolor="#FFFFFF" height="250" name="visualzone" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="" />
					</object>
				</div>
				<div><b>Search</b> results are translating...</div>
				<div>please be patient.</div>
				<div id="marginLoading"></div>
			</div>
		</div>
		<div id="moreBtn">
			<input type="hidden" id="curpage" value="1"/>
			<input type="hidden" id="curpage" value="1"/>
			<span style="float:right;">
				<input type="button" onclick="javascript:viewMore();" value="View more results"/>
			</span>
			<div class="clear"></div>
		</div>
		
	</div>
	
	<div id="footer">
		<span style="float:left">
			<img src="../images/graylogo.png" alt="ESKorea logo" title="ESKorea"/>
		</span>
		<span>Copyright S.O.P.T. All rights reserved.</span>
	</div>
